std::extent

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< class T, unsigned N = 0>
struct extent;
(C++11 feature)

If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array, if N is in [0, std::rank<T>::value). For any other type, or if T is array of unknown bound along its first dimension and N is 0, value is 0.

Contents

Inherited from std::integral_constant

Member objects

value the number of elements along the Nth dimension of T
(public member object)

[edit] Equivalent definition

[edit] Example

#include <iostream>
#include <type_traits>
 
template<typename A>
typename std::enable_if< std::rank<A>::value == 1 >::type print_1d(const A& a)
{
    for(std::size_t i = 0; i<std::extent<A>::value; ++i)
        std::cout << a[i] << ' ';
    std::cout << '\n';
}
 
int main()
{
    int a[][3] = {{1,2,3},{4,5,6}};
//  print_1d(a); // compile-time error
    print_1d(a[1]);
    int* p = a[1];
//  print_1d(p); // compile-time error
}

Output:

4 5 6

[edit] See also

is_array (C++11)
checks if a type is an array type
(class template)
rank (C++11)
obtains the number of dimensions of an array type
(class template)
remove_extent (C++11)
removes one extent from the given array type
(class template)
remove_all_extents (C++11)
removes all extents from the given array type
(class template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages